package com.capinfo.accounting.web;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.capinfo.accumulation.model.accounting.FUZHU_HSQCSZLSSJ;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.FUZHU_HSQCSZLSSJServiceI;
import com.capinfo.accumulation.util.JSONUtil;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;




@Controller
@RequestMapping("/business/accounting/fzhsqcsz")
public class fzhsqcszController{
	@Autowired
	
     private FUZHU_HSQCSZLSSJServiceI fuzhu_hsqcszlssjServiceI;
	
	
	
	@RequestMapping("search.shtml")
	public String toIndex(HttpServletRequest request){  
		
		
        return "business/accounting/fzhsqcsz/search";  
	
	}
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(FUZHU_HSQCSZLSSJ t,HttpSession session,NativeWebRequest request) {
		String entity=request.getParameter("inserted");
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request); 
    	Date d = new Date();
		Json j = new Json();
		
		List l=JSONUtil.toList(entity);
			 for (int i = 0; i < l.size(); i++) {
					Map map = (Map) l.get(i);
				    t.setID(map.get("ID")==null||"".equals(map.get("ID"))?null:Long.parseLong((map.get("ID").toString()).replace(".0", "")));
					t.setCWKM((map.get("KMBH").toString()));
					t.setCZYID(user.getId());
					t.setCRSJ(d);
					t.setHSLX(map.get("HSLXBM")==null||"".equals(map.get("HSLXBM"))?null:Long.parseLong((map.get("HSLXBM").toString())));	
					t.setKJND(map.get("KJND")==null||"".equals(map.get("KJND"))?null:Long.parseLong((map.get("KJND").toString())));
					t.setBNLJJF(map.get("BNLJJF")==null||"".equals(map.get("BNLJJF"))?null:Double.parseDouble( map.get("BNLJJF").toString()));
					t.setBNLJDF(map.get("BNLJDF")==null||"".equals(map.get("BNLJDF"))?null:Double.parseDouble( map.get("BNLJDF").toString()));
					t.setNCYE(map.get("NCYE")==null||"".equals(map.get("NCYE"))?null:Double.parseDouble( map.get("NCYE").toString()));
					t.setYUE(map.get("YUE")==null||"".equals(map.get("YUE"))?null:Double.parseDouble( map.get("YUE").toString()));
					try {
						
						if(t.getID()==null){
							fuzhu_hsqcszlssjServiceI.insert(t);
							j.setSuccess(true);
							j.setMsg("添加成功");
							j.setObj(t);
						}else{
							fuzhu_hsqcszlssjServiceI.updateByPrimaryKeySelective(t);	
							j.setSuccess(true);
							j.setMsg("修改成功");
							j.setObj(t);
							
						}
						
					
					} catch (Exception e) {
						e.printStackTrace();
						j.setMsg(e.getMessage());
					}
				}
		
		return j;
	}
	@RequestMapping(value="/cwkm")  
	@ResponseBody  
	public void cwkm (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		 Gson gson = new Gson();
		   Map map = new HashMap();
		   List<Map<String,Object>> cwkm= fuzhu_hsqcszlssjServiceI.cwkm();
		   PrintWriter pw;
		   pw = response.getWriter();
		   pw.write(gson.toJson(cwkm));
	   }
//	@RequestMapping(value="/hslx")  
//	@ResponseBody  
//	public void hslx (HttpServletRequest request,HttpServletResponse response) throws IOException  {
//		response.setHeader("Content-type", "text/html;charset=UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		 Gson gson = new Gson();
//		   Map map = new HashMap();
//		   List<Map<String,Object>> hslx= fuzhu_hsqcszlssjServiceI.hslx();
//		   PrintWriter pw;
//		   pw = response.getWriter();
//		   pw.write(gson.toJson(hslx));
//	   }
	@RequestMapping(value="/QClist")      
	@ResponseBody	 
     public DataGrid search (HttpServletRequest request,HttpServletResponse response,String kjnd) throws IOException  {
 		response.setHeader("Content-type", "textml;charset=UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		String str= request.getParameter("KJND");
 		String hslx=request.getParameter("HSLX");
 		String cwkm=request.getParameter("CWKM");
           
 		
 		   Map map = new HashMap();
 		   map.put("currentPieceNum", 1);
 		   map.put("perPieceSize", 10);
 		   map.put("sort", "KMBH");
 		   map.put("order", "DESC");
 		   if(str!= null){
 		   map.put("KJND_EQ_NUM",str);
 		   }
 		   if(hslx!= null){
 	 	   map.put("HSLX_EQ_NUM",hslx);
 		   }
 		   if(cwkm!= null){
 	 	 	   map.put("KMBH_EQ_STR",cwkm);
 	 	   }
 		   DataGrid dataGrid=  fuzhu_hsqcszlssjServiceI.findbyByGrid(map);
 		   List jf = dataGrid.getRows();
 		   Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
 		   gson.toJson(dataGrid);
           PrintWriter pw;
 		   pw = response.getWriter();
 		   pw.write(gson.toJson(dataGrid));

 	       return null;
 	   }
	
	
}